package com.example.chat.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.chat.domain.dto.*;
import com.example.chat.domain.vo.UserInfoVo;
import com.example.chat.result.Result;
import com.example.chat.service.MomentService;
import com.example.chat.service.UserService;
import com.example.chat.utils.UserHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/moments")
@CrossOrigin
@Slf4j
@Tag(name = "朋友圈相关接口")
public class MomentController {

    @Resource
    private MomentService momentService;

    @Operation(summary = "发布朋友圈")
    @PostMapping("/add")
    public Result addMoment(@RequestBody MomentFormDTO momentFormDTO){
        return momentService.addMoment(momentFormDTO.getContent(),
                momentFormDTO.getImgs(),momentFormDTO.getScope());
    }

    @Operation(summary = "滚动查询公共朋友圈")
    @GetMapping("/queryPublic")
    public Result queryPublicMoment(Long timestamp, Integer offset){
        return momentService.queryPublicMoment(timestamp,offset);
    }

    @Operation(summary = "滚动查询个人朋友圈")
    @GetMapping("/queryPrivate")
    public Result queryPrivate(Long id,Long timestamp, Integer offset){
        return momentService.queryPrivate(id,timestamp,offset);
    }

    @Operation(summary = "朋友圈点赞")
    @PostMapping("/likes")
    public Result likes(Long id,Integer type){
        String s = type == 1 ? "点了赞" : "取消点赞";
        log.info("ID为{}的用户对ID为{}的动态{}", UserHolder.getCurrentId(),id,s);
        return momentService.likes(id,type);
    }

    @Operation(summary = "发布朋友圈评论")
    @PostMapping("/comments")
    public Result comments(@RequestBody CommentFormDTO commentFormDTO){
        log.info("ID为{}的用户在ID为{}的动态下发布了评论", UserHolder.getCurrentId(),commentFormDTO.getId());
        return momentService.comments(commentFormDTO.getId(),commentFormDTO.getText());
    }

    @Operation(summary = "查询某条朋友圈信息及评论")
    @GetMapping("/getComments")
    public Result getCommentById(Long momentId){
        log.info("ID为{}的用户查看ID为{}的动态评论", UserHolder.getCurrentId(),momentId);
        return momentService.getCommentById(momentId);
    }

    @Operation(summary = "滚动查询用户动态")
    @GetMapping("/queryUserMoment")
    public Result queryUserMoment(Long id,Long momentId,Integer type){
        log.info("ID为{}的用户浏览动态", UserHolder.getCurrentId());
        return momentService.queryUserMoment(id,momentId,type);
    }

    @Operation(summary = "根据类型发布不同动态")
    @PostMapping("/add/type")
    public Result addMomentByType(@RequestBody MomentFormDTO momentFormDTO){
        log.info("ID为{}的用户发布了类型为{}的动态", UserHolder.getCurrentId(),momentFormDTO.getType());
        return momentService.addMomentByType(momentFormDTO.getContent(),
                momentFormDTO.getImgs(),momentFormDTO.getFile(),momentFormDTO.getType());
    }

    @Operation(summary = "隐藏或删除动态")
    @PutMapping("/deleteOrHide")
    public Result DeleteOrHide(@RequestBody DeleteOrHideDTO deleteOrHideDTO){
        String s = "";
        Integer type = deleteOrHideDTO.getType();
        if(type != 0){
            s = type == 1 ? "显示了" : "隐藏了";

        }else{
            s = "删除了";
        }
        log.info("ID为{}的用户{}动态{}", UserHolder.getCurrentId(),s,deleteOrHideDTO.getId());
        return momentService.DeleteOrHide(deleteOrHideDTO.getId(),deleteOrHideDTO.getType());
    }
}
